<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2020/1/4
// +----------------------------------------------------------------------

namespace app\command;

use Carbon\Carbon;
use think\console\Input;
use think\console\Output;
use think\Db;

/**
 * 删除昨天的用户行为记录 每天 03：35运行
 * Class ClearUserBehaviorLogCommand
 * @package app\command
 * @author feraner<feraner@163.com>
 * @date 2020/1/4
 */
class ClearUserBehaviorLogCommand extends BasicCommand
{

    protected $name = 'behavior:clear';

    protected $desc = '清理用户行为记录命令';


    public function __construct($name = null)
    {
        parent::__construct($name);
    }

    /**
     * @param Input  $input
     * @param Output $output
     *
     * @return int|null|void
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    protected function execute(Input $input, Output $output)
    {

        while (true) {
            $result = Db::name('user_behavior_log')
                ->where('status', 1)
                ->where('created_at', '>=', Carbon::yesterday()->getTimestamp())
                ->where('created_at', '<=', Carbon::yesterday()->endOfDay()->getTimestamp())
                ->limit(500)
                ->delete();
            if (!$result) {
                break;
            }
        }

    }


}
